<script setup>
import {UploadOutlined} from "@ant-design/icons-vue";
import axios from "axios";
import {useRouter} from "vue-router";
import {message} from "ant-design-vue";
import {useMenuStore} from "../../store/menu.js";
import server from "../../config/server.js";

const store = useMenuStore()
const router = useRouter()

const customRequest = (option) => {
  const formData = new FormData();
  const fileUrl = `${server.apiBaseUrl}/upload`;

  formData.append('file[]', option.file);
  axios.postForm(fileUrl, {
    file: option.file,
  }).then(res => {
    console.log(res)
    const respData = res.data.data
    console.log("respData", respData)
    const data = {
      "name": respData.origin_name,
      "uuid": respData.uuid,
      "md5": respData.md5_value,
      "size": respData.size,
      "suffix": respData.suffix,
      "path": respData.path,
    }
    axios.post(`${server.apiBaseUrl}/zdppy_amfile`, data).then(resp2 => {
      store.setSelectedKeys(["file"])
      router.push("/file/index")
      message.success("上传文件成功")
    })
  })
}
</script>

<template>
  <a-upload
      name="file"
      :customRequest="customRequest"
  >
    <a-button>
      <upload-outlined></upload-outlined>
      文件上传
    </a-button>
  </a-upload>
</template>